package com.twu.biblioteca;

import javax.security.auth.login.FailedLoginException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: dbelsey
 * Date: 29/01/13
 * Time: 14:48
 */
public class Menu {
    private List <MenuItem> menuContents;

    public Menu(){
        menuContents = new ArrayList <MenuItem>();

        menuContents.add(new MenuItem("Exit Application"){
            public String runMenuItem(BibliotecaAppData runningState, IOHandler io){
                runningState.stop();
                return "Exiting Application. Thank you for using Biblioteca";
            }
        });

        menuContents.add(new MenuItem("Show Book List"){
            public String runMenuItem(BibliotecaAppData bibliotecaAppData, IOHandler io){
                return System.getProperty("line.separator") + bibliotecaAppData.getLibraryInventory().viewBookList();

            }
        });

        menuContents.add(new MenuItem("Show Movie List"){
            public String runMenuItem(BibliotecaAppData bibliotecaAppData, IOHandler io){
                return System.getProperty("line.separator") + bibliotecaAppData.getLibraryInventory().viewMovieList();
            }
        });

        menuContents.add(new MenuItem("Reserve Book"){
            public String runMenuItem(BibliotecaAppData bibliotecaAppData, IOHandler io){
                if(bibliotecaAppData.isUserLoggedIn()){
                    while (true) {
                        io.sendOutput("Type number of the book you would like to reserve: ");
                        int selectedBookNumber = io.getInputNumber();
                        try {
                            if (bibliotecaAppData.getLibraryInventory().reserveBook(selectedBookNumber))
                                return ("Thank You! Enjoy the book.");
                            else
                                return ("Sorry we don't have that book yet.");
                        }
                        catch (InvalidLibraryItemNumberException e) {
                            io.sendOutput(e.getMessage());
                        }
                        catch (NumberFormatException e) {
                            io.sendOutput("Select a valid option!!");
                        }
                    }
                }
                else {
                    io.sendOutput("Sorry, you must be logged in to reserve a book");
                    return "";
                }
            }
        });

        menuContents.add(new MenuItem("Check Membership Details"){
            public String runMenuItem(BibliotecaAppData bibliotecaAppData, IOHandler io){
                if(bibliotecaAppData.isUserLoggedIn()){
                    // TODO: and now it can :) continue when refactor finished
                    UserData currentUsersData = bibliotecaAppData.getLoggedInUserData();
                    String userDataString = "Name: " + currentUsersData.getName();
                    userDataString = userDataString + System.getProperty("line.separator") + "Email: " + currentUsersData.getEmailAddress();
                    userDataString = userDataString + System.getProperty("line.separator") + "Phone: " + currentUsersData.getPhoneNumber();
                    return userDataString;
                }
                else return "Please talk to a Librarian. Thank you.";
            }
        });


        menuContents.add(new MenuItem("Login"){
            public String runMenuItem(BibliotecaAppData bibliotecaAppData, IOHandler io){
                io.sendOutput("\nEnter details below to log in");
                io.sendOutputPrint("Username: ");
                String userName = io.getInputString();
                io.sendOutputPrint("Password: ");
                String password = io.getInputString();
                try{
                    bibliotecaAppData.logInUser(userName, password);
                    return "Login successful. Welcome " + bibliotecaAppData.getLoggedInUserFullName() + ".";
                }
                catch(FailedLoginException e){
                    io.sendOutput(e.getMessage());
                    return "";
                }
            }
        });

    }

    public List <String> getMenuContents() {
        ArrayList <String> menuAsStringList = new ArrayList <String>();
        // for loop used here instead of iterating over whole list so we can present the exit option (index 0) to the user last
        for(int i = 1; i < menuContents.size(); i++) {
           menuAsStringList.add(i + ". " + menuContents.get(i).toString());
        }
        menuAsStringList.add("0. " + menuContents.get(0).toString());
        return menuAsStringList;
    }

    public MenuItem selectMenuItem(int menuOption)throws InvalidMenuSelectionException{
        if (menuOption < menuContents.size() && menuOption >= 0)
            return menuContents.get(menuOption);
        else throw new InvalidMenuSelectionException();
    }

    public int getMenuLength(){
        return menuContents.size();
    }
}